#include <stdio.h>
#include "i2c_aw99703.h"
#include "pinmux.h"
#include "drv_gpio.h"
#include "i2c_base.h"
#include "drv_delay.h"
#include "aos/hal/gpio.h"

#define AW99703_HWEN_PIN   8        // AOGPIO8
#define AW99703_PWM_PIN    PC23     // PWM CH2

int aw99703_io_init(void)
{
    gpio_pin_handle_t pin = NULL;
    int ret;
	hal_aogpio_output_high(AW99703_HWEN_PIN);
    
    // drv_pinmux_config(PWM_CH2_MUX, GPIO_MUX);
    drv_pinmux_config(AW99703_PWM_PIN, PC23_PWM_CH2);      // PA[29:26]
    pin = csi_gpio_pin_initialize(AW99703_PWM_PIN, NULL);
    if(pin == NULL) {
        printf("csi_gpio_pin_initialize() PWM error.\r\n");
        return -1;
    }
    ret = csi_gpio_pin_config_direction(pin, GPIO_DIRECTION_OUTPUT);
    ret |= csi_gpio_pin_write(pin, 1);
    if (ret) {
        printf("cfg or write pin error.\r\n");
        return -1;        
    }
    csi_gpio_pin_uninitialize(pin);    

    return 0;
}



static int aw99703_read_chipid(uint8_t *read_data)
{
    int32_t ret;
    
    ret = lcd_i2c_read(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_ID, read_data);

    return ret;
}



int aw99703_init(uint16_t backlight_code, uint8_t bl_ch_msk)
{
    int ret;
    uint8_t id;
    uint8_t reg;
    uint8_t flag1,flag2;

    ret = aw99703_io_init();
    if (ret) {
        printf("aw99703_io_init rtn err.\r\n");
        return -1;       
    }
    mdelay(5);  // delay 5ms after chip enable

    ret = aw99703_read_chipid(&id);
    if (!ret)
        ;//printf("AW99703 id %d\r\n", id);
    else
        printf("AW99703 get id err.\r\n");

    reg = AW99703_MODE_PDIS_DISABLE | AW99703_MODE_MAP_LINEAR
        | AW99703_MODE_WORKMODE_STANDBY;
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_MODE, reg);

    reg = (0x13 << AW99703_LEDCUR_BLFS_SHIFT) | (bl_ch_msk & 0x07);
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
    	AW99703_REG_LEDCUR, reg);

    if( backlight_code > AW99703_MAX_BL_CODE)
        backlight_code = AW99703_MAX_BL_CODE;
    reg = backlight_code & 0x07;    // LSB
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_LEDLSB, reg);
    reg = (backlight_code >> 3) & 0xFF;    // MSB
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_LEDMSB, reg); 

    reg = AW99703_BSTCTR2_IDCTSEL_10UH;
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_BSTCTR2, reg); 

    reg = AW99703_MODE_PDIS_DISABLE | AW99703_MODE_MAP_LINEAR
        | AW99703_MODE_WORKMODE_BACKLIGHT;
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_MODE, reg);

    ret |= lcd_i2c_read(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_FLAGS1, &flag1);
    ret |= lcd_i2c_read(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_FLAGS2, &flag2);
    //printf("AW99703 flag1 0x%x flag2 0x%x\r\n", flag1, flag2);

    return ret;
}


/* code: 0 ~ 2047, 0 turn off */
int aw99703_adj_bklgt(int backlight_code)
{
    int ret = 0;
    uint8_t reg;

    if( backlight_code > AW99703_MAX_BL_CODE)
        backlight_code = AW99703_MAX_BL_CODE;
    reg = backlight_code & 0x07;    // LSB
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_LEDLSB, reg);
    reg = (backlight_code >> 3) & 0xFF;    // MSB
    ret |= lcd_i2c_write(g_handle_bklgt_pwr, AW99703_IIC_ADDR,
        AW99703_REG_LEDMSB, reg); 

    return ret;
}
